package at.codebase.core;

import at.codebase.security.SecureStringBuilder;

/**
 * @author Daniel Marth <danielmarth@gmx.at>
 */
public class PageLinkFactory {

	/**
	 * @param baseUrl
	 *            URL with the template "{page}" for the page to link to.
	 */
	public static String createLinks(final String baseUrl,
			final int currentPage, final int lastPage) {
		String urlTemplate = new SecureStringBuilder()
				.appendInsecure("<a href=\"").appendInsecure(baseUrl)
				.appendInsecure("\">{linkName}</a>").toString();
		String container = "<div class=\"linkFactoryContainer\">";
		
		if (currentPage == 1) {
			container += buildFirstPage(urlTemplate, currentPage, lastPage);
		} else if (currentPage == lastPage) {
			container += buildLastPage(urlTemplate, currentPage, lastPage);
		} else {
			container += buildMiddlePage(urlTemplate, currentPage, lastPage);
		}
		
		return container += "</div>";
	}

	private static String buildFirstPage(final String urlTemplate,
			final int currentPage, final int lastPage) {
		if (lastPage == 1) {
			return "<< < 1 > >>";
		}
		SecureStringBuilder builder = new SecureStringBuilder();
		builder.appendInsecure("<< < 1 ");
		for (int i = 2, j = 0; i < lastPage && j < 2; i++, j++) {
			builder.appendInsecure(urlTemplate.replace("{linkName}",
					String.valueOf(i)).replace("{page}", String.valueOf(i)));
			builder.appendInsecure(" ");
		}
		if ((lastPage - currentPage) > 3) {
			builder.appendInsecure("... ");
		}
		builder.appendInsecure(urlTemplate.replace("{linkName}",
				String.valueOf(lastPage)).replace("{page}",
				String.valueOf(lastPage)));
		builder.appendInsecure(" ");
		builder.appendInsecure(urlTemplate.replace("{linkName}", ">").replace(
				"{page}", String.valueOf(currentPage + 1)));
		builder.appendInsecure(" ");
		builder.appendInsecure(urlTemplate.replace("{linkName}", ">>").replace(
				"{page}", String.valueOf(lastPage)));
		return builder.toString();
	}

	private static String buildLastPage(final String urlTemplate,
			final int currentPage, final int lastPage) {
		SecureStringBuilder builder = new SecureStringBuilder();
		builder.appendInsecure(" ");
		builder.appendInsecure(String.valueOf(lastPage));
		builder.appendInsecure(" >> >");
		for (int i = lastPage - 1, j = 0; i > 1 && j < 2; i--, j++) {
			builder.appendFrontInsecure(urlTemplate.replace("{linkName}",
					String.valueOf(i)).replace("{page}", String.valueOf(i)));
			builder.appendFrontInsecure(" ");
		}
		if (lastPage > 4) {
			builder.appendFrontInsecure(" ...");
		}
		builder.appendFrontInsecure(urlTemplate.replace("{linkName}", "1")
				.replace("{page}", "1"));
		builder.appendFrontInsecure(" ");
		builder.appendFrontInsecure(urlTemplate.replace("{linkName}", "<")
				.replace("{page}", String.valueOf(currentPage - 1)));
		builder.appendFrontInsecure(" ");
		builder.appendFrontInsecure(urlTemplate.replace("{linkName}", "<<")
				.replace("{page}", "1"));
		return builder.toString();
	}

	private static String buildMiddlePage(String urlTemplate, int currentPage,
			int lastPage) {
		SecureStringBuilder builder = new SecureStringBuilder();
		builder.appendInsecure(urlTemplate.replace("{linkName}", "<<")
				.replace("{page}", "1"));
		builder.appendInsecure(" ");
		builder.appendInsecure(urlTemplate.replace("{linkName}", "<")
				.replace("{page}", String.valueOf(currentPage - 1)));
		builder.appendInsecure(" ");
		builder.appendInsecure(urlTemplate.replace("{linkName}", "1")
				.replace("{page}", "1"));
		builder.appendInsecure(" ");
		if (currentPage > 4) {
			builder.appendInsecure("... ");
		}
		SecureStringBuilder beforeBuilder = new SecureStringBuilder();
		for (int i = currentPage - 1, j = 0; i > 1 && j < 2; i--, j++) {
			beforeBuilder.appendFrontInsecure(" ");
			beforeBuilder.appendFrontInsecure(urlTemplate.replace("{linkName}",
					String.valueOf(i)).replace("{page}", String.valueOf(i)));
		}
		builder.append(beforeBuilder);
		builder.appendInsecure(String.valueOf(currentPage));
		builder.appendInsecure(" ");
		for (int i = currentPage + 1, j = 0; i < lastPage && j < 2; i++, j++) {
			builder.appendInsecure(urlTemplate.replace("{linkName}",
					String.valueOf(i)).replace("{page}", String.valueOf(i)));
			builder.appendInsecure(" ");
		}
		if ((lastPage - currentPage) > 3) {
			builder.appendInsecure("... ");
		}
		builder.appendInsecure(urlTemplate.replace("{linkName}",
				String.valueOf(lastPage)).replace("{page}",
				String.valueOf(lastPage)));
		builder.appendInsecure(" ");
		builder.appendInsecure(urlTemplate.replace("{linkName}", ">").replace(
				"{page}", String.valueOf(currentPage + 1)));
		builder.appendInsecure(" ");
		builder.appendInsecure(urlTemplate.replace("{linkName}", ">>").replace(
				"{page}", String.valueOf(lastPage)));
		return builder.toString();
	}

}
